package com.qdlc.p2p.manage.action.additional;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BaseGiveAwardService;
import com.qdlc.p2p.biz.service.UserWineCouponsService;
import com.qdlc.p2p.biz.service.WineCouponsService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.UserWineCouponsCategory;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.UserWineCouponsModel;

/**
 * 用户酒券管理
 * 
 * @author ygw
 * @version 1.1.0.0
 * @date 2016年5月6日 下午2:57:12
 */
public class ManageUserWineCouponsAction extends BaseAction<UserWineCouponsModel> implements
		ModelDriven<UserWineCouponsModel> {
	@Resource
	private  WineCouponsService wineCouponsService;
	@Resource
	private UserWineCouponsService UserWineCouponsService;

	@Resource
	@Qualifier("giveUserWineCouponsService")
	BaseGiveAwardService giveUserWineCouponsService;
	
	private UserWineCouponsModel model = new UserWineCouponsModel();
	
	public UserWineCouponsModel getModel() {
		
		return model;
	}
	
	private Map<String, Object> data = new HashMap<String, Object>();
	
	/**
	 * 用户酒券管理 页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/additional/wineCoupons/userWineCoupons/userWineCouponsManager")
	public String UserWineCouponsManager() throws Exception {
		return "userWineCouponsManager";
	}
	
	/**
	 * 用户酒券列表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/additional/wineCoupons/userWineCoupons/userWineCouponsList")
	public void UserWineCouponsList() throws Exception {
		// 初始化status值为99
		String status = paramString("status");
		if (StringUtil.isBlank(status)) 
			model.setStatus((byte) 99);
		
		PageDataList<UserWineCouponsModel> pageDataList = UserWineCouponsService.list(model);
		int total = pageDataList.getPage().getTotal();
		data.put("total", total);
		data.put("rows", pageDataList.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 导出用户酒券报表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/additional/wineCoupons/userWineCoupons/exportExcelUserWineCouponsList")
	public void exportExcelUserWineCouponsList() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		model.setPage(1);
		model.setRows(5000);
		// 获取记录
		PageDataList<UserWineCouponsModel> pageDataList = UserWineCouponsService.list(model);
		
		String title = "用户酒券获取记录Excel表";
		
		String[] hearders = new String[] { "编号", "用户名", "酒券名称", "酒券金额","酒券类型", "状态", "有效天数","过期时间","使用时间","获取时间", "酒券来源", "使用类型" };// 表头数组
		String[] fields = new String[] { "id", "userName", "name", "amount", "typeStr", "statusStr", "validDay", "expiredTime","useTime","addTime","sourceType","useTypeStr"};// 对象属性数组
		TableData td = ExcelUtils.createTableData(pageDataList.getList(), ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}
	
	/**
	 * 置用户酒券为无效
	 * 
	 * @throws Exception
	 */
	@Action("/modules/additional/wineCoupons/userWineCoupons/doUserWineCouponsFailure")
	public void doUserWineCouponsFailure() throws Exception {
		UserWineCouponsService.doInvalid(model.getId());
		printResult(MessageUtil.getMessage("MS101002001"), true);
	}
	/**
	 * 发放酒券页面
	 */
	@Action("/modules/additional/wineCoupons/userWineCoupons/append")
	public String append(){
		request.setAttribute("usablewineCouponsTypeList", wineCouponsService.findUsableWineCoupons());
		return "append";
	}
	/**
	 * 用户添加酒券
	 */
	@Action("/modules/additional/wineCoupons/doAppend")
	public void doAppend() throws Exception{
		data = new HashMap<String, Object>();
		//后台无法给用户名<4的用户发酒券
		//model.validModelData();
		model.setKeyId(UserWineCouponsCategory.VERIFY_FULL_BORROW_SUCCESS);
		giveUserWineCouponsService.giveAward(model.prototypeForAppend());
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS101001001"));
		printJson(getStringOfJpaObj(data));
	}
}
